#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 18-4-8 下午12:44
# @Author  : huanggz
# @File    : music163.py
# @Software: PyCharm
# code is far away from bugs with the god animal protecting
from selenium import webdriver
import time,requests
from logzero import logger
def get_singer_list(uid):
    driver = webdriver.Chrome()
    try:
        driver.get(f'http://music.163.com/artist/album?id={ uid }&limit=1000' )
        time.sleep(2)
        driver.switch_to.frame(driver.find_element_by_id('g_iframe'))
        ul=driver.find_elements_by_css_selector('ul#m-song-module > li ')
        return_list={}
        for i in ul:
            title=i.find_element_by_tag_name('div').get_attribute('title')
            href = i.find_element_by_css_selector('a.msk').get_attribute('href')
            return_list[title]=href
            return return_list
    except Exception as e:
        print(e)
        logger.error(e)
    finally:
        driver.quit()

def get_sing_list_by_url(url):
    driver = webdriver.Chrome()
    try:
        driver.get(url)
        time.sleep(2)
        driver.switch_to.frame(driver.find_element_by_id('g_iframe'))
        ul = driver.find_elements_by_css_selector('table.m-table >tbody> tr ')
        return_list = {}
        for i in ul:
            href = i.find_element_by_tag_name('a').get_attribute('href')
            title = i.find_element_by_tag_name('b').get_attribute('title')
            # return return_list
    except Exception as e:
        print(e)
    finally:
        driver.quit()
if __name__ == '__main__':
    # get_singer_list(3701)
    get_sing_list_by_url('http://music.163.com/album?id=11302')
    # content=requests.get('http://music.163.com/album?id=11302').text
    # print(content)